package com.ecnu.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecnu.common.annotation.Log;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.entity.SysMenu;
import com.ecnu.common.core.domain.entity.SysRole;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.common.enums.BusinessType;
import com.ecnu.common.utils.SecurityUtils;
import com.ecnu.system.domain.ProjectMessage;
import com.ecnu.system.domain.dto.MyMessageDTO;
import com.ecnu.system.domain.vo.RouterVo;
import com.ecnu.system.service.IProjectMessageService;
import com.ecnu.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "后台管理")
@RestController
@RequestMapping("/backstage")
public class BackStageController extends BaseController {
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private IProjectMessageService projectMessageService;

    /**
     * 后台管理菜单
     *
     * @return
     */
    @GetMapping("getList")
    @ApiOperation(value = "后台菜单")
    public AjaxResult getList() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        boolean containsStudent = roles.stream()
                .anyMatch(role -> "student".equals(role.getRoleKey()));

        List<SysMenu> menus = menuService.selectMenuForBackStage(containsStudent);
//        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        List<RouterVo> routerVos = menuService.buildMenus(menus);
        routerVos.forEach(routerVo -> {
            routerVo.setMeta(null);
            routerVo.setComponent("PmsLayout");
            routerVo.setAlwaysShow(null);
        });
        return AjaxResult.success(routerVos);
    }


    /**
     * 我的消息
     */
    @Log(title = "我的消息", businessType = BusinessType.OTHER)
    @ApiOperation(value = "我的消息")
    @PostMapping("/getMyMessage")
    public TableDataInfo getMyMessage(@Valid @RequestBody MyMessageDTO dto) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        LoginUser loginUser = getLoginUser();
        return projectMessageService.selectList(dto,loginUser);
    }

    /**
     * 消息提醒
     */
    @Log(title = "我的消息--明细", businessType = BusinessType.OTHER)
    @ApiOperation(value = "消息--明细")
    @GetMapping("/getMessage")
    public AjaxResult getMessage(@RequestParam("id") String id) {

        LoginUser loginUser = getLoginUser();
        ProjectMessage id1 = projectMessageService.getOne(new QueryWrapper<ProjectMessage>()
                .eq("id", id));
        if (id1 != null ) {
            if(id1.getType().equals("1")){
                new Thread(() -> {
                    ProjectMessage message = new ProjectMessage();
                    message.setId(id);
                    message.setStatus("1");
                    message.setUpdateUser(loginUser.getUserId().toString());
                    projectMessageService.updateById(message);
                }).start();
            }
        } else {
            id1 = new ProjectMessage();
        }
        return AjaxResult.success(id1);
    }
}
